<template>
<div>
    <van-datetime-picker v-model="currentDate" type="date" title="选择年月日" :min-date="minDate" :max-date="maxDate" @cancel="$emit('close')" @confirm="save" />
</div>
</template>

<script>
import dayjs from 'dayjs'
import { updateUserProfile } from '@/api/user'
export default {
  name: 'UpdateBirthday',
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      minDate: new Date(1960, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.value)
    }
  },
  methods: {
    async save (value) {
      console.log(value)
      // 先把这个value这个对象变成字符串
      var val = dayjs(value).format('YYYY-MM-DD')
      this.$emit('input', val)

      try {
        await updateUserProfile({ birthday: val })
        this.$toast.success('更新生日成功')
        this.$emit('close')// 关闭弹出层
      } catch (err) {
        this.$toast.fail('更新生日成功')
      }
    }
  }
}
</script>

<style scoped lang="less">

</style>
